<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="../common/NavigationLeft.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>固定资产管理系统</title>
</head>
<body style="height: 100%">
	<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
		<!-- 	  <nav class="navbar  navbar-fixed-static" bo> -->
		<!-- 	  		<h5 style="font-size:20px"><b>资产列表</b></h5> -->
		<!-- 	  </nav> -->
		<h2 class="page-header">用户列表</h2>
		<div class="row m-b-sm m-t-sm">
			<div class="col-md-1">
				<a><button id="refresh" type="button"
						class="btn btn-default btn-sm">刷新</button></a>
			</div>
			<form class="form-horizontal" action="#" method="post"
				id="searchForm">
				<div class="col-md-5">
					<button class="btn btn-default btn-sm" type="button"
						id="deleteMany_btn">删除</button>
					<a href="toAddUser.action"><button type="button"
							class="btn btn-default btn-sm" id="entry_btn">添加</button></a>
					<button type="button" class="btn btn-default btn-sm"
						id="batch_add_btn">批量添加</button>
					<button type="button" class="btn btn-default btn-sm"
						id="download_btn" onclick="download()">导出用户表格</button>
				</div>
				<div class="col-md-4 f-r">
					<div class="input-group">
						<input type="text" id="queryName" class="form-control"
							value="${queryName}" name="queryName" placeholder="请输入用户名"
							style="height: 30px;"> <span class="input-group-btn">
							<button class="btn btn-primary btn-sm" type="button"
								id="search_btn">搜索</button>
							<button class="btn btn-sm" type="button" id="more_search_btn">更多</button>
						</span>
					</div>
				</div>
				<input id="pageCurr_hi" name="pageSizeCurr" type="hidden"
					value="${pageSizeCurr}" /> <input id="pageAll_hi"
					name="pageSizeALL" type="hidden" value="${pageSizeALL}" />
			</form>
		</div>
		<form class="form-horizontal search_table">
			<div class="form-group col-md-4 col-sm-6 col-xs-12">
				<label class="col-sm-4 control-label">添加日期</label>
				<div class="col-sm-8 WdatePicker">
					<input id="addTime1" class="form-control" type="datetime"
						placeholder="开始时间"
						onclick="WdatePicker({maxDate:'%y-%M-%d || #F{$dp.$D(\'addTime2\')}'})" />
					- <input id="addTime2" class="form-control" type="datetime"
						placeholder="结束时间"
						onclick="WdatePicker({minDate:'#F{$dp.$D(\'addTime1\')}',maxDate:'%y-%M-%d'})" />
				</div>
			</div>
			<div class="form-group col-md-12 text-center m-t-sm">
				<button id="complex_search_btn" type="button"
					class="btn btn-sm btn-primary">查询</button>
				<button id="clear_btn" type="button" class="btn btn-sm btn-default ">清空</button>
			</div>
		</form>
		<div class="placeholder"></div>
		<form class="form-horizontal" action="" method="post" id="user_Form"
			target="_blank">
			<table id="dataTable"
				class="table table-bordered table-striped table-hover"
				cellspacing="0">
				<thead>
					<tr>
						<th><input autocomplete="off" type="checkbox" id="CheckedAll"
							onclick="checkAll()"></th>
						<th>用户名</th>
						<th class="min-tablet-p dropdown selectdown">
							<div class="table_dropdown dropdown">
								<a data-toggle="dropdown" class="dropdown-toggle"
									href="javascript:void(0);" id="role">角色 <span class="caret"></span></a>
								<ul role="menu" class="dropdown-menu">
									<li class="active role_filter"><a
										href="javascript:void(0);">全部</a></li>
									<li value="0" class="role_filter"><a
										href="javascript:void(0);">管理员</a></li>
									<li value="1" class="role_filter"><a
										href="javascript:void(0);">普通用户</a></li>
								</ul>
							</div>
						</th>
						<th>添加时间</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
				</tbody>
			</table>
		</form>
	</div>
	<div class="edit-detail layer_panel" id="File_upload_panel">
		<form action="/asset/addUserBatch.action" method="post"
			id="loadFileForm" enctype="multipart/form-data">
			<div>
				<label class="col-sm-12 control-label">请选择文件（.csv文件或.xls文件）</label>
			</div>
			<div class="col-sm-12">
				<s:file class="col-sm-12" type="file" name="file" id="file"
					onchange="changeFileName()"></s:file>
			</div>
			<div style="display: none">
				<p class='col-sm-12' style='color: red'>请选择文件!</p>
			</div>
			<input type="hidden" id="fileName" name="fileName" value="" />
		</form>
	</div>
	<input id="userRole" type="hidden" value="${session.user.role}">
</body>
<script type="text/javascript">
	$(function() {
		var table = "";
		initTable();

		var tip = "${tip}";
		if (tip != "") {
			layer.msg(tip, {
				area : [ '300px', '50px' ]
			});
		}

		$("#search_btn").click(function(){
			 initTable();
		});
		
		$("#refresh").click(function() {
			window.location.reload();
		})

		$(".search_table").hide();
		$("#more_search_btn").click(function() {
			$(".search_table").toggle();
		});

		$("#complex_search_btn").click(function() {
			initTable();
		});

		$("#clear_btn").click(function() {
			$(".search_table input").each(function() {
				$(this).val("");
			})
		});

		$(document).on(
				"click",
				".dropdown .dropdown-menu a",
				function() {
					$(this).parent().addClass("active").siblings().removeClass(
							'active');
					$(".table_dropdown").removeClass("open");
					var role = $(this).parent().attr("value");
					initTable(role);
				});

		function initTable(role) {
			var queryName = $("#queryName").val();
			var addTime1 = $("#addTime1").val();
			var addTime2 = $("#addTime2").val();
			$.fn.dataTable.ext.errMode = function(s, h, m) {
				layer.alert('数据加载失败');
			}
			queryData = {
				"queryName" : queryName,
				"role" : role,
				"addTime1" : addTime1,
				"addTime2" : addTime2
			}
			table = $('#dataTable').DataTable(
				{
					"destroy" : true,
				    "paging" : true,
					"lengthChange" : true,
					"searching" : false,
					"ordering" : false,
				    "info" : true,
					"autoWidth" : false,
					"Filter" : false,
					responsive : true,
					"pagingType" : "full_numbers",
					"dom" : 'rtilp',
					"language" : {
						"url" : "/static/js/datatables/jquery.dataTables.zh"
								},
					"processing" : true,
					"serverSide" : true,
					"ajax" : {
						type : 'POST',
						url : 'ajax_listUserData.action',
						data : queryData,
						timeout : 60000
							},
							"drawCallback" : function() {							
								$(".del_li").click(function() {
									var assetId = $(this).attr("assetId");
									layer.confirm(
										'确认删除该用户？',
										{
											icon : 3,
											title : '删除用户'
										},
										function() {
											$.ajax({
													type : 'POST',
													url : 'ajax_deleteUser.action',
													data : {
															"id" : assetId
															},
															success : function(result) {
																		if (result == "success") {
																				table.page(table.page()).draw(false);
																					layer.msg('删除成功');

																		} else {
																				table.page(table.page()).draw(false);
																					layer.msg('删除失败');
                                                                                             }
																						},
															error : function(result) {
																		table.page(table.page()).draw(false);
																				layer.msg('删除失败,系统错误');
																				}
																	});
															});
													});

									$("#queryName").keydown(function() {
														var event = window.event || arguments.callee.caller.arguments[0];
														if (event.keyCode == 13) {
															table.page(table.page()).draw(false);
														}
													});
								},
								"columns" : [
										{
											/* 此处data指定的是数据库该行的属性,如果没有指定的话,则代表整行数据 */
											"data" : "id",
											render : function(data, display,
													row) {
												if (row.status == "Inner") {
													return '<input  name="checkedIds" type="checkbox" disabled="disabled" value="'+ data +'">';
												} else {
													return '<input  name="checkedIds" type="checkbox" value="'+ data+'">';
												}
											}
										},
										{
											"data" : "name",
											render : function(data) {
												return data;
											}
										},
										{
											"data" : "role",
											render : function(data) {
												if (data == "Admin") {
													return "管理员";
												}
												if (data == "User") {
													return "普通用户";
												}

											}
										},
										{
											"data" : "addedTime",
											render : function(data) {
												return data;
											}
										},
										{
											"data" : function(data) {

												var modifyOpe = "";
												var deleteOpe = "";
												/* 在session会话中的到登陆者身份 */
												var userRole = $("#userRole").val();

												if (userRole == "0") {

													modifyOpe = '<li><a class="" href="toModifyAddUser.action?id='+ data.id + '" onclick="">修改</a></li>';
													deleteOpe = '<li><a class="del_li" href="javascript:void(0);" assetId="' + data.id + '">删除</a></li>';
												}
												return '<div class="btn-group">' + '<button data-toggle="dropdown"'+
												          'class="btn btn-primary btn-xs dropdown-toggle" type="button">'
														+ '<i class="fa fa-cog"></i> 操作 <span class="caret"></span>'
														+ '</button>'
														+ '<ul class="dropdown-menu">'
														+ '<li><a class="" href="userInfo.action?id='+ data.id + '">查看</a></li>'
														+ modifyOpe + deleteOpe
												'</ul>' + '</div>';
											}
										}

								]
							});
			return table;
		}
	});

	$("#deleteMany_btn").click(function() {
		var ids = [];
		$("input[name=checkedIds]:checked").each(function() {
			var id = $(this).attr("value");
			ids.push(id);
		});
		layer.confirm('确认删除选中的用户？', {
			icon : 3,
			title : '删除用户'
		}, function(index) {
			layer.close(index);
			$.ajax({
				type : 'POST',
				url : "ajax_deleteUserMany.action",
				data : {
					"ids" : ids,
				},
				traditional : true,
				success : function() {
					window.location.reload();
				},
				error : function() {
					$("#search_btn").click();
					layer.msg("删除失败，系统错误");
				}
			});
		});
	});

	$("input[name=checkedIds]")
			.click(
					function() {
						var allboxNum = $("input[name=checkedIds]").length;
						var disabledBoxNum = $("input[name=checkedIds][disabled=disabled]").length;
						var checkedBoxNum = $("input[name=checkedIds]:checked").length;
						if (allboxNum == (checkedBoxNum + disabledBoxNum)) {
							$("#CheckedAll").prop("checked", true);
						} else {
							$("#CheckedAll").prop("checked", false);
						}
					});
	function checkAll() {
		if ($("#CheckedAll").prop("checked")) {
			$(":checkbox").prop("checked", true);
			$("input[name=checkedIds][disabled=disabled]").prop("checked",
					false);
		} else {
			$(":checkbox").prop("checked", false);
		}
	}
	function download() {
		window.location.href = "exportUserExcel.action";
	}

	$(function() {
		var userRole = $("#userRole").val();
		if (userRole == "1") {
			$("#deleteMany_btn").attr("disabled", "disabled");
			$("#entry_btn").attr("disabled", "disabled");
			$("#batch_add_btn").attr("disabled", "disabled");
			$("#download_btn").attr("disabled", "disabled");
		}
	});
	//定义的方法：选择文件
	function changeFileName() {
		var filePath = $("#file").val();
		var fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
		$("#fileName").val(fileName);
		$("#file").parent().next().hide();
	}

	$("#batch_add_btn").click(function() {
		layer.open({
			title : '文件批量导入',
			type : 1,
			area : [ '400px', '200px' ],
			content : $("#File_upload_panel"),
			btn : [ '确定', '取消' ],
			yes : function(index, layero) {
				if ($("#file").val() != '') {
					$("#loadFileForm").submit();
				} else {
					$("#file").parent().next().show();
				}
			},
			cancel : function(index) {
			}
		});
	});
</script>
</html>